package com.woniu.controller;


import com.woniu.model.dto.RefundDto;
import io.swagger.annotations.Api;
import cn.hutool.core.bean.BeanUtil;
import com.alipay.api.AlipayApiException;
import com.woniu.commons.exception.YogaException;
import com.woniu.commons.model.Result;
import com.woniu.commons.util.JwtUtil;
import com.woniu.commons.util.RedisUtil;
import com.woniu.model.form.AgreeRefundForm;
import com.woniu.model.form.ApplyForm;
import com.woniu.model.model.YGoodsOrder;
import com.woniu.model.model.YRefund;
import com.woniu.model.param.ApplyParam;
import com.woniu.service.YRefundService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 胡歌
 * @since 2024年05月28日
 */
@RestController
@RequestMapping("/yRefund")
@Api(tags = "退款接口")
public class YRefundController {

    @Resource
    private YRefundService refundService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 用户申请退款
     * @param form
     * @param token
     * @return
     */
    @PostMapping("/apply")
    @ApiOperation("用户申请退款")
    public Result apply(@RequestBody ApplyForm form, @RequestHeader String token){
        String refreshToken = redisUtil.get(token);
        int userId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("userId").toString());
        ApplyParam param = BeanUtil.toBean(form, ApplyParam.class);
        param.setUserId(userId);
        refundService.apply(param);
        return Result.getResult().setMsg("申请退款成功");
    }

    /**
     * 查询申请退款
     * @return
     */
    @GetMapping("/loadAllRefund")
    @ApiOperation("查询申请退款")
    public Result<List<RefundDto>> loadAllRefund(Integer refundState){
        List<RefundDto> list= refundService.loadAllRefund(refundState);
        return Result.newListInstance(RefundDto.class).setData(list).setMsg("查询申请退款成功");
    }


    /**
     * 用户查询申请退款
     * @return
     */
    @GetMapping("/selctRefund")
    @ApiOperation("用户查询申请退款详情")
    public Result<List<YRefund>> selctRefund(){
        List<YRefund> list= refundService.selctRefund();
        return Result.newListInstance(YRefund.class).setData(list).setMsg("用户查询申请退款详情成功");
    }





    /**
     * 管理员同意退款 需要的参数 及后面的调用支付宝其他方法
     * @param orderNum 退款订单
     * @return
     * @throws AlipayApiException
     */
    @GetMapping("/agreeRefund")
    @ApiOperation("管理员同意申请退款")
    public Result agreeRefund(String orderNum)throws AlipayApiException {
//        String refreshToken = redisUtil.get(token);
//        int userId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());
//        form.setRefundAdminId(userId);
        refundService.sendRefundQueue(orderNum);
        return Result.getResult().setMsg("申请退款成功");
    }

    /**
     * 管理员驳回退款
     * @param refundId
     * @return
     * @throws AlipayApiException
     */
    @GetMapping("/rejectRefund")
    @ApiOperation("管理员驳回申请退款")
    public Result rejectRefund(Integer refundId)throws AlipayApiException {

        refundService.rejectRefund(refundId);
        return Result.getResult().setMsg("驳回退款申请成功");
    }




}

